<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	    <meta name="viewport" content="width=device-width,initial-scale=1.0"> 
		<meta name="author" content="test">
		<meta name="description" content="test">
		<meta name="copyright" content="test">
		<title>demo</title>
	</head>
	<body>
		<input id="loginBtn" type="button" value="login"/>
		<input id="getImagesBtn" type="button" value="获取图片数据"/>
		<input id="loginOutBtn" type="button" value="loginOut"/>
		<script type="text/javascript" src="../js/jquery/jquery-1.11.3.js"></script>
		<script>
			(function(){
				var contextPath = '/pic';
				// 登录
				document.getElementById('loginBtn').addEventListener('click',function(){
					$.ajax({
						url: contextPath + '/app/userLogin',
						data: $.extend({'userName':'hzy','password':'hzy1234'},getCommonParams()),
						success: function(data){
							data = data || {};
							window.localStorage.setItem('app_token',data.token);
							window.localStorage.setItem('app_user',JSON.stringify(data.user));
							alert('登录成功');
						},
						error: function(){
							alert('登录失败');
						}
					});
				});
				document.getElementById('getImagesBtn').addEventListener('click',function(){
					$.ajax({
						url: contextPath + '/app/search/doSearch',
						data: $.extend({},getCommonParams()),
						success: function(data,xhr,a,b,c){
							data = data || [];
							console.log('data:' + JSON.stringify(data));
						},
						error: function(xhr)
						{
							if(xhr && xhr.responseText == '10001'){
								alert('账号已失效，请重新登录');
							}else{
								alert('获取图片失败');
							}	
						},
					});
				});
				
				$('#loginOutBtn').bind('click',function(){
					$.ajax({
						url: contextPath + '/app/userLoginOut',
						data:$.extend({},getCommonParams()),
						success: function(data,xhr,a,b,c){
							alert('已退出登录');
						},
						error: function(xhr)
						{
							alert('推出登录失败');
						},
					});
				});
				
				function getCommonParams(){
					return {'token':window.localStorage.getItem('app_token'),'deviceid':'huawei-honor-8-88877'};
				}
			})();
		</script>
	</body>
</html>